'use strict';

import React, {Component, PropTypes} from 'react';
import {ModalTrigger, Modal} from 'amazeui-react';

export default class Popup extends Component {
	/*
	 * 构造函数。
	 */
	constructor(props) {
		super(props);
		this.state = {
			show: false,
		};
		this.show = this.show.bind(this);
		this.close = this.close.bind(this);
	}

	/*
	 * 显示模态框。
	 *
	 */
	show() {
		this.setState({
			show: true
		});
	}

	/*
	 * 关闭模态框。
	 */
	close() {
		this.refs.modal.close();
	}
	
	/*
	 * 模态框关闭回调。
	 */
	OnClose() {
		this.setState({
			show: false
		});
		this.props.onClose();
	}

	/*
	 * 渲染。
	 */
	render() {
		let modal = (
			<Modal 
				closeViaDimmer={this.props.closeViaDimmer}
				type="popup"
				title={this.props.title}
			>{this.props.children}</Modal>
		);
		
		return (
			<ModalTrigger 
				modal={modal}
				show={this.state.show}
				onClose={this.OnClose.bind(this)}
				ref="modal"
			/>
		);
	}
}

Popup.propTypes = {
	title: PropTypes.string,
	closeViaDimmer: PropTypes.bool,
	onClose: PropTypes.func,
};
Popup.defaultProps = {
	onClose: () => {},
};
